import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/entity/shop.dart';

class ShopDetailGoodsWidget extends StatefulWidget {
  final Shop shop;

  ShopDetailGoodsWidget({
    Key key,
    this.shop
  }) : super(key: key);

  @override
  _ShopDetailGoodsWidgetState createState() {
    return _ShopDetailGoodsWidgetState();
  }
}

class _ShopDetailGoodsWidgetState extends State<ShopDetailGoodsWidget> {
  List<Foods> _allFoods = [];

  @override
  void initState() {
    super.initState();
    _allFoods.clear();
    widget.shop.goods.forEach((goods) {
      if (goods.foods.length > 0) {
        goods.foods[0].goods = goods;
      }
      _allFoods.addAll(goods.foods);
    });
  }
  @override
  Widget build(BuildContext context) {
    return _buildShopGoodsContent();
  }

  Widget _buildShopGoodsContent() {
    return Row(
      children: <Widget>[
        Container(
          width: 80,
          child: ListView.separated(
              itemCount: widget.shop.goods.length,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                    child: Text(widget.shop.goods[index].name),
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  height: 0.5,
                  color: Colors.black12,
                );
              }
          ),
        ),
        Expanded(
          child: _buildGoodsListView(),
        )
      ],
    );
  }

  // 构建分类列表
  Widget _buildGoodsListView() {
    return ListView.separated(
        itemBuilder: (BuildContext context, int index) {
          return _buildFoodItem(_allFoods[index]);
        },
        separatorBuilder: (BuildContext context, int index) {
          return Container(
            height: 0.5,
            color: Colors.black12,
          );
        },
        itemCount: _allFoods.length
    );
  }

  // 构建商品列表
  Widget _buildFoodItem(Foods foods) {
    return Column(
      children: <Widget>[
        Container(
          color: Colors.black12,
          width: double.infinity,
          child: foods.goods == null ? null : Padding(
            padding: const EdgeInsets.symmetric(vertical: 3, horizontal: 15),
            child: Text(foods.goods.name),
          ),
        ),
        InkWell(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                CachedNetworkImage(
                  imageUrl: foods.image,
                  width: 80,
                  height: 64,
                ),
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(left: 8),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(foods.name, style: TextStyle(fontSize: 16, color: Colors.black87),),
                        Text(foods.description),
                        Text('月售${foods.sellCount} 好评${foods.ratings.length}'),
                        Row(
                          children: <Widget>[
                            Text('￥${foods.price}', style: TextStyle(color: Colors.red, fontSize: 18, fontWeight: FontWeight.bold),),
                            Expanded(
                              flex: 1,
                              child: Container(),
                            ),
                            Icon(Icons.shopping_cart, color: Theme.of(context).primaryColor,)
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
